package com.yt.base.controller.app;

import com.google.common.base.Strings;
import com.yt.app.core.interfaces.AppTopicCategoryService;
import com.yt.app.core.model.AppTopicCategory;
import com.yt.base.controller.core.BaseController;
import com.yt.core.MsgBean;
import com.yt.core.dao.PagingBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/app/topicCategory")
public class AppTopicCategoryController extends BaseController{

    @Autowired
    private AppTopicCategoryService appTopicCategoryService;

    @RequestMapping("list")
    public ModelAndView list(@RequestParam(value = "start", required = false) String start,
                             @RequestParam(value = "limit", required = false) String limit){
        ModelAndView mav=new ModelAndView("/app/topicCategory/list");
        QueryFilter filter=new DynamicSQLFilterImpl();
        filter.setSortField("id",true);
        PagingBean page = new PagingBean();
        int currentPage = 0;
        int pageSize = 2;
        if (StringUtils.isNotEmpty(limit) && StringUtils.isNumeric(limit)) {
            pageSize = Integer.parseInt(limit);
        }
        if (StringUtils.isNotEmpty(start) && StringUtils.isNumeric(start)) {
            currentPage = (Integer.parseInt(start) - 1) * pageSize;
        }
        page.setPageSize(pageSize);
        page.setStart(currentPage);

        List<AppTopicCategory> list= appTopicCategoryService.find(filter);

        int total=appTopicCategoryService.count(filter);
        mav.addObject("total",total);
        mav.addObject("list",list);
        return mav;
    }

    @RequestMapping("toAdd")
    public ModelAndView toAdd(){
        ModelAndView mav=new ModelAndView("/app/topicCategory/add");

        return mav;
    }


    @RequestMapping("toEdit")
    public ModelAndView toEdit(@RequestParam(value = "id",required = true) Long id){
        ModelAndView mav=new ModelAndView("/app/topicCategory/add");
        AppTopicCategory topicCategory=appTopicCategoryService.get(id);
        mav.addObject("obj",topicCategory);
        return mav;
    }

    @RequestMapping("save")
    public @ResponseBody Object save(@RequestParam(value = "id",required = false)Long id,
                                     @RequestParam(value = "name",required = false)String name){
        MsgBean msg=new MsgBean();
        AppTopicCategory topicCategory=null;
        if(id!=null && id>0){
            topicCategory=appTopicCategoryService.get(id);
        }

        if(topicCategory==null){
            topicCategory=new AppTopicCategory();
        }
        if(Strings.isNullOrEmpty(name)){
            msg.setFailure(1,"名称不能为空！");
            return msg.returnMsg();
        }else if(name.length()>20){
            msg.setFailure(1,"名称不能超过20个字符！");
            return  msg.returnMsg();
        }
        topicCategory.setName(name.trim());

        if(topicCategory.getId()!=null && topicCategory.getId()>0){
            appTopicCategoryService.update(topicCategory);
        }else{
            appTopicCategoryService.insert(topicCategory);
        }
        return msg.returnMsg();
    }

    @RequestMapping("/multiDel")
    @ResponseBody
    public Object multiDel(@RequestParam("ids") String ids) {
        MsgBean msg = new MsgBean();
        String[] idArr = ids.split(",");
        if (ids != null) {
            for (String id : idArr) {
                appTopicCategoryService.remove(Long.valueOf(id));
            }
        }
        return msg.returnMsg();
    }


}
